# provides controlls and UI for the addition of owners to a project team
class OwnersController < ApplicationController
  include EnotifyLdap
  layout 'admin_standard'
  before_filter :authenticate
  
  active_scaffold :owner
  active_scaffold :owner do |config|
     config.columns = [:owner_name]
     config.list.per_page = 100
     config.list.sorting = [{ :person => :desc}]
     config.actions.exclude :update, :show, :create
     config.columns[:owner_name].label = "Team Members"
  end

  
  # GET /owners
  # GET /owners.xml
  def index
    @owners = Owner.find(:all )

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @owners }
    end
  end

  # GET /owners/1
  # GET /owners/1.xml

  def show
    @owners = Owner.find_all_by_project_id(params[:id])

    respond_to do |format|
      format.html { render :scaffold => @owners }# show.html.erb
      format.xml  { render :xml => @owner }
    end
  end

  # GET /owners/new
  # GET /owners/new.xml
  def new
    @owner = Owner.new
    @team = Team.find(params[:id])
    @project = @team.project
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @owner }
    end
  end

  # GET /owners/1/edit
  def edit
    @owner = Owner.find(params[:id])
  end

  # POST /owners
  # POST /owners.xml
  def create
      logger.info 'stared create'
      
      @team = Team.find(params[:owner][:team_id])
      @project = @team.project
      
      logger.info 'stared person find'
      ## get the person entity
      #@person = Person.find_or_create_by_username(params[:owner][:username])
      @person = get_person_from_ldap_by_username(params[:owner][:username])

      ## the person will be nil if they were not found in LDAP.
      if !@person.nil?
        @person.save
      
        logger.info 'stared making owner'
        ## add the pointers to the team and person that make up this owner
        @owner = Owner.new
        @owner.team = @team
        @owner.person = @person
      
      
        logger.info 'respond_to'

        respond_to do |format|
          logger.info 'start owner save'
          if @owner.save
            logger.info 'Owner saved'
            flash[:notice] = 'Added ' + params[:owner][:username].to_s  + ' to ' + @team.manager.full_name + "'s team."
            logger.info "flash added"
            format.html { redirect_to(:controller => '/teams', :action => :show, :id => @team.id) }
          else
            flash[:notice] = 'Something went wrong.'
            format.html { render :action => "new" }
          end
        end
      else  #if person actuall is nil
        respond_to do |format|
           flash[:notice] = 'The username you provided does not belong to one of our employees. Do not freak out! This message is NOT endorsed by the Human Resources Department.'
           format.html { render :action => "new" }
           format.xml  { render :xml => @administrator.errors, :status => :unprocessable_entity }
        end
      end
        
        
  end

  # PUT /owners/1
  # PUT /owners/1.xml
  def update
    @owner = Owner.find(params[:id])

    respond_to do |format|
      if @owner.update_attributes(params[:owner])
        flash[:notice] = 'Owner was successfully updated.'
        format.html { redirect_to(@owner) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @owner.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /owners/1
  # DELETE /owners/1.xml
  def destroy
    
    @owner = Owner.find(params[:id])
    @team = @owner.team
    @owner.destroy

    flash[:notice] = 'Owner was successfully deleted. Please Refresh the page.'

    respond_to do |format|
      format.html { redirect_to(:controller => '/teams', :action => :show, :id => @team.id) }
      format.xml  { head :ok }
    end
  end

end
